package com.zqq.user.ui.activity

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zqq.baselibrary.ext.enable
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.provider.router.RouterPath
import com.zqq.user.R
import com.zqq.user.injection.component.DaggerUserComponent
import com.zqq.user.injection.module.UserModule
import com.zqq.user.presenter.ForgetPwdPresenter
import com.zqq.user.presenter.view.ForgetPwdView
import kotlinx.android.synthetic.main.activity_forget_pwd.*
import org.jetbrains.anko.toast

/**
 * 忘记密码界面
 * wc
 * @author zqq on 2019/5/24.
 */
@Route(path = RouterPath.UserCenter.PATH_FORGET_PWD)
class ForgetPwdActivity : BaseMvpActivity<ForgetPwdPresenter>(), ForgetPwdView, View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forget_pwd)
        initView()
    }

    private fun initView() {
        mNextBtn.enable(mMobileEt) { isBtnEnable() }
        mNextBtn.enable(mVerifyCodeEt, { isBtnEnable() })

        mVerifyCodeBtn.onClick(this)
        mNextBtn.onClick(this)
    }

    override fun injectComponent() {
        DaggerUserComponent.builder()
                //因为UserCom依赖于ActivityCom所以需要ActivityCom实例，且其在BaseMvpActivity已实例化
                .activityComponent(mActivityComponent)
                .userModule(UserModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.mVerifyCodeBtn -> {
                mVerifyCodeBtn.requestSendVerifyNumber()
                toast("发送验证成功")
            }

            R.id.mNextBtn -> {
                mPresenter.forgetPwd(mMobileEt.text.toString(), mVerifyCodeEt.text.toString())
            }
        }
    }

    //判断按钮是否可用
    private fun isBtnEnable(): Boolean {
        return mMobileEt.text.isNullOrEmpty().not() &&
                mVerifyCodeEt.text.isNullOrEmpty().not()
    }

    /**-----------------接口返回---------------------*****/
    override fun onForgetPwdResult(result: String) {
        toast(result)
        ARouter.getInstance()
                .build(RouterPath.UserCenter.PATH_RESET_PWD)
                .withString(RouterPath.UserCenter.ROUTE_KEY_FORGET, mMobileEt.text.toString())
                .navigation()
    }

}